import Vue from "vue";
import Vuex from "vuex";
import admin from "./admin/index.js";
import user from "./user/index"
import axios from "axios";
Vue.use(Vuex);

const state = {
  islogin: localStorage.islogin,
  isadmin: localStorage.isadmin,
  loading: false,           //数据是否加载完成
  pageSum: 1,               //设置总页数
  pageIndex: 1              //设置页数下标
}

const mutations = {
  //是否加载完成
  CHANGE_LOADING(state, bool) {
    state.loading = bool;
  },
  //修改总页数
  CHANGE_PAGE_SUM(state, pageSum) {
    state.pageSum = pageSum;
  },
  //设置下标
  CHANGE_PAGE_INDEX(state, pageIndex) {
    state.pageIndex = pageIndex;
  },
  //管理员登录与token
  CHANGE_ADMIN_INFO(state, data) {
    state.islogin = localStorage.islogin = true;
    state.isadmin = localStorage.isadmin = true;
    state.admin.adminName = localStorage.adminName = data.adminName;
    state.admin.admintoken = localStorage.admintoken = data.admintoken;
  },
  //用户登录与token
  CHANGE_USER_INFO(state, data) {
    state.islogin = localStorage.islogin = true;
    state.isadmin = localStorage.isadmin = false;
    state.user.userName = localStorage.userName = data.userName;
    state.user.usertoken = localStorage.usertoken = data.usertoken;
    state.user.user_id = localStorage.user_id = data.user_id;
  },
  //退出登录
  OUT_LOGIN(state) {
    localStorage.clear();   //清除localStorage
    state.islogin = false;
  }
}

const actions = {
  //管理员登录
  adminlogin(state, that) {
    return axios.post("/adminlogin", that.ruleForm)
  },
  //用户登录
  userlogin(state, that) {
    return axios.post("/userLogin", that.userFrom)
  },
}

const modules = {
  admin,
  user
}

export default new Vuex.Store({
  state,
  mutations,
  actions,
  modules
});
